package com.idealighter.game.roomcard.games.mustniuniu;

import com.idealighter.game.games.common.niuniu.NiuNiuCard;
import com.idealighter.game.roomcard.games.mustniuniu.constant.MustNiuNiuCardType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 牌型比较工具类 .
 * 
 * @date 2015年9月16日 下午10:09:03
 *
 */
public class MustNiuNiuCardsTypeComparator {
  private MustNiuNiuCardsTypeComparator() {}

  /**
   * 比较大小 .
   * 
   * @param myCards .
   * @param myCardsType .
   * @param otherCards .
   * @param otherCardsType .
   * @return true:我大 false：庄家大
   */
  public static boolean compare(List<NiuNiuCard> myCards, MustNiuNiuCardType myCardsType,
      List<NiuNiuCard> otherCards, MustNiuNiuCardType otherCardsType) {

    if (myCardsType.niu > otherCardsType.niu) {
      return true;
    } else if (myCardsType.niu < otherCardsType.niu) {
      return false;
    } else {
      // 先比较特殊牌型
      // 炸弹牛，只比较炸弹大小|葫芦牛，只比较三张相同牌大小
      if (myCardsType == MustNiuNiuCardType.NIU_ZHA || myCardsType == MustNiuNiuCardType.HU_LU) {
        NiuNiuCard myMaxCard = getMaxCardOfNiuZhaOrHuLu(myCards, myCardsType);
        NiuNiuCard dealerMaxCard = getMaxCardOfNiuZhaOrHuLu(otherCards, otherCardsType);

        if (myMaxCard.power > dealerMaxCard.power) {
          return true;
        } else if (myMaxCard.power < dealerMaxCard.power) {
          return false;
        } else {
          return myMaxCard.type.ordinal() > dealerMaxCard.type.ordinal();
        }
      } else if (myCardsType == MustNiuNiuCardType.WUXIAO //
          || myCardsType == MustNiuNiuCardType.WU_HUA //
          || myCardsType == MustNiuNiuCardType.TONG_HUA //
          || myCardsType == MustNiuNiuCardType.SHUN_ZI) {
        NiuNiuCard myMaxCard = getMaxCard(myCards);
        NiuNiuCard dealerMaxCard = getMaxCard(otherCards);

        if (myMaxCard.power > dealerMaxCard.power) {
          return true;
        } else if (myMaxCard.power < dealerMaxCard.power) {
          return false;
        } else {
          return myMaxCard.type.ordinal() > dealerMaxCard.type.ordinal();
        }
      }

      // 同牌型的有王的比没王的小
      if (!myCards.contains(NiuNiuCard.DA_WANG) && !myCards.contains(NiuNiuCard.XIAO_WANG)
          && (otherCards.contains(NiuNiuCard.DA_WANG)
              || otherCards.contains(NiuNiuCard.XIAO_WANG))) {
        return true;
      }

      if (!otherCards.contains(NiuNiuCard.DA_WANG) && !otherCards.contains(NiuNiuCard.XIAO_WANG)
          && (myCards.contains(NiuNiuCard.DA_WANG) || myCards.contains(NiuNiuCard.XIAO_WANG))) {
        return false;
      }

      NiuNiuCard myMaxCard = getMaxCard(myCards);
      NiuNiuCard dealerMaxCard = getMaxCard(otherCards);

      if (myMaxCard.power > dealerMaxCard.power) {
        return true;
      } else if (myMaxCard.power < dealerMaxCard.power) {
        return false;
      } else {
        return myMaxCard.type.ordinal() > dealerMaxCard.type.ordinal();
      }
    }
  }

  private static boolean compareAllCards(List<NiuNiuCard> myCards, List<NiuNiuCard> otherCards) {
    myCards.sort((cards1, cards2) -> {
      if (cards1.equals(cards1)) {
        return 0;
      }
      if (cards1.power > cards2.power) {
        return 1;
      }
      if (cards1.power < cards2.power) {
        return -1;
      }
      if (cards1.type.ordinal() > cards2.type.ordinal()) {
        return 1;
      }
      return 0;
    });


    otherCards.sort((cards1, cards2) -> {
      if (cards1.equals(cards1)) {
        return 0;
      }
      if (cards1.power > cards2.power) {
        return 1;
      }
      if (cards1.power < cards2.power) {
        return -1;
      }
      if (cards1.type.ordinal() > cards2.type.ordinal()) {
        return 1;
      }
      return 0;
    });
    for (int i = 0; i < 5; i++) {
      if (myCards.get(i).equals(otherCards.get(i))) {
        continue;
      }
      if (myCards.get(i).power > otherCards.get(i).power) {
        return true;
      } else if (myCards.get(i).power < otherCards.get(i).power) {
        return false;
      } else {
        return myCards.get(i).type.ordinal() > otherCards.get(i).type.ordinal();
      }
    }
    return false;
  }

  /**
   * @Title compareByAllCard.
   * @Description 比牌
   * @date 2018年6月14日 下午4:22:31
   * @return .
   */
  public static boolean compareByAllCard(List<NiuNiuCard> myCards, MustNiuNiuCardType myCardsType,
      List<NiuNiuCard> otherCards, MustNiuNiuCardType otherCardsType) {

    if (myCardsType.niu > otherCardsType.niu) {
      return true;
    } else if (myCardsType.niu < otherCardsType.niu) {
      return false;
    } else {
      // 先比较特殊牌型
      // 炸弹牛，只比较炸弹大小|葫芦牛，只比较三张相同牌大小
      if (myCardsType == MustNiuNiuCardType.NIU_ZHA || myCardsType == MustNiuNiuCardType.HU_LU) {
        NiuNiuCard myMaxCard = getMaxCardOfNiuZhaOrHuLu(myCards, myCardsType);
        NiuNiuCard dealerMaxCard = getMaxCardOfNiuZhaOrHuLu(otherCards, otherCardsType);

        if (myMaxCard.equals(dealerMaxCard)) {
          return compareAllCards(myCards, otherCards);
        }

        if (myMaxCard.power > dealerMaxCard.power) {
          return true;
        } else if (myMaxCard.power < dealerMaxCard.power) {
          return false;
        } else {
          return myMaxCard.type.ordinal() > dealerMaxCard.type.ordinal();
        }
      } else if (myCardsType == MustNiuNiuCardType.WUXIAO //
          || myCardsType == MustNiuNiuCardType.WU_HUA //
          || myCardsType == MustNiuNiuCardType.TONG_HUA //
          || myCardsType == MustNiuNiuCardType.SHUN_ZI) {
        NiuNiuCard myMaxCard = getMaxCard(myCards);
        NiuNiuCard dealerMaxCard = getMaxCard(otherCards);

        if (myMaxCard.equals(dealerMaxCard)) {
          return compareAllCards(myCards, otherCards);
        }

        if (myMaxCard.power > dealerMaxCard.power) {
          return true;
        } else if (myMaxCard.power < dealerMaxCard.power) {
          return false;
        } else {
          return myMaxCard.type.ordinal() > dealerMaxCard.type.ordinal();
        }
      }

      // 同牌型的有王的比没王的小
      if (!myCards.contains(NiuNiuCard.DA_WANG) && !myCards.contains(NiuNiuCard.XIAO_WANG)
          && (otherCards.contains(NiuNiuCard.DA_WANG)
              || otherCards.contains(NiuNiuCard.XIAO_WANG))) {
        return true;
      }

      if (!otherCards.contains(NiuNiuCard.DA_WANG) && !otherCards.contains(NiuNiuCard.XIAO_WANG)
          && (myCards.contains(NiuNiuCard.DA_WANG) || myCards.contains(NiuNiuCard.XIAO_WANG))) {
        return false;
      }

      NiuNiuCard myMaxCard = getMaxCard(myCards);
      NiuNiuCard dealerMaxCard = getMaxCard(otherCards);

      if (myMaxCard.equals(dealerMaxCard)) {
        return compareAllCards(myCards, otherCards);
      }

      if (myMaxCard.power > dealerMaxCard.power) {
        return true;
      } else if (myMaxCard.power < dealerMaxCard.power) {
        return false;
      } else {
        return myMaxCard.type.ordinal() > dealerMaxCard.type.ordinal();
      }
    }
  }

  /**
   * @Title getMaxNumCard.
   * @Description 获取最多数量的牌
   * @date 2018年5月24日 下午12:00:13
   * @param cards .
   * @param type .
   * @return .
   */
  private static NiuNiuCard getMaxCardOfNiuZhaOrHuLu(List<NiuNiuCard> cards,
      MustNiuNiuCardType type) {
    NiuNiuCard result = null;
    if (type == MustNiuNiuCardType.NIU_ZHA || type == MustNiuNiuCardType.HU_LU) {
      Map<Integer, List<NiuNiuCard>> map = new HashMap<Integer, List<NiuNiuCard>>();
      for (NiuNiuCard card : cards) {
        List<NiuNiuCard> list = map.get(card.power);
        if (list == null) {
          list = new ArrayList<NiuNiuCard>();
        }
        list.add(card);
        map.put(card.power, list);
      }
      // 取出数量最多的牌
      List<NiuNiuCard> tmpList = null;
      for (Integer power : map.keySet()) {
        List<NiuNiuCard> list = map.get(power);
        if (tmpList == null) {
          tmpList = list;
        } else {
          if (list.size() > tmpList.size()) {
            tmpList = list;
          }
        }
      }
      // 从数量最多的牌中取出最大值
      for (NiuNiuCard item : tmpList) {
        if (result == null) {
          result = item;
        } else {
          if (item.power > result.power) {
            result = item;
          } else if (item.power == result.power) {
            if (item.type.ordinal() > result.type.ordinal()) {
              result = item;
            }
          }
        }
      }
    }
    return result;
  }

  /**
   * 获取最大的牌 .
   * 
   * @param cards .
   * @return .
   */
  private static NiuNiuCard getMaxCard(List<NiuNiuCard> cards) {
    NiuNiuCard maxCard = null;
    for (NiuNiuCard card : cards) {
      if (card.equals(NiuNiuCard.XIAO_WANG) || card.equals(NiuNiuCard.DA_WANG)) {
        continue;
      }
      if (maxCard == null || card.power > maxCard.power
          || (card.power == maxCard.power && card.type.ordinal() > maxCard.type.ordinal())) {
        maxCard = card;
      }
    }
    return maxCard;
  }



}
